package net.sf.cldsql.dj.utl;

/**
 * FixExtractor
 */
public class XfixParser {
    public static final boolean PREFIX = true;
    public static final boolean SUFFIX = false;
    private final boolean type;
    private final String splitter;
    private String otherPart;
    private boolean found;
    
    public XfixParser (boolean type, String splitter) {
        this.type = type;
        this.splitter = splitter;
    }
    
    public boolean parse(String str, String fix) {
        if (type == PREFIX) {
            String combined = fix + splitter;
            if (str.startsWith(combined)) {
                this.otherPart = str.substring(combined.length());
                this.found = true;
            } else {
                this.otherPart = null;
                this.found = false;
            }
        } else if (type == SUFFIX) {
            String combined = splitter + fix;
            if (str.endsWith(splitter + fix)) {
                this.otherPart = str.substring(0, str.length() - combined.length());
                this.found = true;
            } else {
                this.otherPart = null;
                this.found = false;
            }
        }
        return found;
    }

    /**
     * @return found
     */
    public boolean isFound() {
        return found;
    }

    /**
     * @return withoutFix
     */
    public String getOtherPart() {
        return otherPart;
    }

}
